package com.xxx.video.login.presenters;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.xxx.video.R;
import com.xxx.video.commen.beans.BaseBean;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

import java.util.regex.Pattern;


public class ForgetPwdSecondPresenter extends BasePresenter<IForgetPwdSecondView> {

    public ForgetPwdSecondPresenter(@NonNull Context context, IForgetPwdSecondView baseView) {
        super(context, baseView);
    }

    /**
     * forget pwd
     *
     * @param userName user name
     */
    public void forgetPassword(String userName) {
        String pwd = getBaseView() == null ? "" : getBaseView().getPwd();
        String pwdAgain = getBaseView() == null ? "" : getBaseView().getPwdAgain();
        if (TextUtils.isEmpty(userName)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_second_username_can_not_empty));
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_second_pwd_can_not_empty));
            return;
        }
        if (TextUtils.isEmpty(pwdAgain) || !TextUtils.equals(pwd, pwdAgain)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_second_pwd_again_not_queals));
            return;
        }
        boolean isMatch = checkPwd(pwd);
        if (!isMatch) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_register_first_pwd_format_error));
            return;
        }
        if (TextUtils.isEmpty(pwdAgain) || !TextUtils.equals(pwd, pwdAgain)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_second_pwd_again_not_queals));
            return;
        }
        NetWorkUtils.getInstance().forgetPassword(userName, pwd, new CommonResponseListener<BaseBean>() {
            @Override
            public void onSuccess(BaseBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().forgetPwdSuccess();
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }

    public boolean checkPwd(String pwd) {

        Pattern numPattern = Pattern.compile("[0-9]+");
        Pattern lowerPattern = Pattern.compile("[a-z]+");
        Pattern upperPattern = Pattern.compile("[A-Z]+");
        if (numPattern.matcher(pwd).find() && lowerPattern.matcher(pwd).find() && upperPattern.matcher(pwd).find()) {
            return true;
        }
        return false;
    }
}
